import React, { useCallback, useState, useEffect, useMemo } from "react";
import { LibAppView } from "../../../../lib";
import { App, Button, Card, Form, Input, Select, Switch, Transfer } from "antd";

import { FormattedMessage, useIntl } from "react-intl";
import EditModules from "./EditModules";
import { EditModuleColumns, EditUserColumns, EditMenuColumn } from "./EditModuleData";
import EditPage, { createFields } from "../../Component/ResourceList/Edit";
import { CONST_ACTIVITY_APPLY_TASK_USERS } from "@/config/constant";

const EditGroup = () => {

    const journeyData = LibAppView.useJourneyData();
   
    const [ defaultData ] = useState(journeyData.currentStepState || {})
    const [ defaultFormData ] = useState({
        ...(journeyData.currentStepState || {}),
        moduleList: defaultData.moduleList || [],
        userList: defaultData.userList || [],
        status: typeof defaultData.status === "boolean" ? defaultData.status : true
    });
    const formFields = createFields([
        {
            label: "用户组",
            name: "name",
            type: "Input",
            "buildProps": () => {
                return {
                    disabled: defaultFormData?.id?.length > 0
                };
            }
        }, {
            label: "admin.pages.user.group.list.module",
            name:"moduleList",
            type: "Custom",
            "Component": EditModules,
            "fullColumn": true,
            props: {
                tableColumns: EditModuleColumns,
                endPoint: "user.moduleList"
            }
        }, {
            label: "后台页面",
            name: "menuList",
            type: "Custom",
            "Component": EditModules,
            "fullColumn": true,
            props: {
                tableColumns: EditMenuColumn,
                endPoint: "menus.fullList",
                expandColumns: EditMenuColumn,
                expandSourceName: "items",
                enableExpandChecked: true,
                formatRequest: (data: any) => {
                    return {
                        ...data,
                        module: "Admin"
                    };
                },
            }
        }, {
            label: "授权用户",
            name: "userList",
            type: "Custom",
            "Component": EditModules,
            "fullColumn": true,
            props: {
                tableColumns: EditUserColumns,
                searchColumns: ([
                    { label: "登录名", type: "Input", name: "loginName" },
                    { label: "用户昵称", type: "Input", name: "userName" }
                ] as Admin.IAdvancedField[]),
                endPoint: "user.list"
            }
        }, {
            label: "admin.pages.user.list.status",
            name: "status",
            type: "Switch",
        }
    ]);

    return <EditPage
        submitEndPoint="user.groupSubmit"
        fields={formFields}
        initValues={{
            status: 0
        }}
        buildRequest={(formData: any) => {
            sessionStorage.removeItem(CONST_ACTIVITY_APPLY_TASK_USERS);
            return {
                ...formData,
                status: /^[\d]+$/.test(formData.status) ? formData.status : 0,
                menuList: formData.menuList?.map((item: any) => {
                    return {
                        ...item,
                        editable: item.editable ? 1 : 0,
                        deleteable: item.deleteable ? 1 : 0,
                        downloadable: item.downloadable ? 1 : 0,
                        parentId: item.parentId || item.parent_id || 0
                    };
                }) || [],
            };
        }}
        buildResponse={(response: any) => {
            return {
                ...response,
                isRef: response.isRef === 1
            }
        }}
    />

};

export default EditGroup;
